﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using DataRecv.tools;
using DataRecv.db;
using System.Data;
using DataRecv.enums;
using System.Collections;

namespace DataRecv.models
{
    /// <summary>
    /// 违法代码
    /// </summary>
    public class VioParam
    {
        /// <summary>
        /// 表名称
        /// </summary>
        private const string c_TableName = "VioParam_t";
        /// <summary>
        /// 字段集合
        /// </summary>
        private const string c_Columns = "viotype,viocode,viodescribe,bz";
        /// <summary>
        /// 字段数量
        /// </summary>
        private const int c_ColumnsCount = 4;
        /// <summary>
        /// 违法类型
        /// </summary>
        public Enum_VioType viotype;
        /// <summary>
        /// 违法代码
        /// </summary>
        public string viocode;
        /// <summary>
        /// 描述
        /// </summary>
        public string viodescribe;
        /// <summary>
        /// 备注
        /// </summary>
        public string bz;
        /// <summary>
        /// 空构造
        /// </summary>
        public VioParam()
        {
            viotype = Enum_VioType.卡口;
            viocode = "";
            viodescribe = "";
            bz = "";
        }

        /// <summary>
        /// 违法代码列表
        /// </summary>
        /// <returns>违法代码列表</returns>
        public static Hashtable GetModels()
        {
            Hashtable ans = ans = new Hashtable();
            DataTable dt = null;
            try
            {
                dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "1=1");
                if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0))
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        VioParam dm = new VioParam();
                        dm.viotype = (Enum_VioType)Enum.Parse(typeof(Enum_VioType), row["viotype"].ToString());
                        dm.viocode = row["viocode"].ToString();
                        dm.viodescribe = row["viodescribe"].ToString();
                        dm.bz = row["bz"].ToString();
                        ans.Add(dm.viotype, dm);
                    }
                }
            }
            catch (Exception er)
            {
                ans = null;
                LogTool.log.Info("VioParam.GetModels()" + er.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
            return ans;
        }

        /// <summary>
        /// 保存参数到数据库
        /// </summary>
        /// <param name="paramname">参数名</param>
        /// <param name="paramvalue">参数值</param>
        /// <returns>保存结果</returns>
        public bool Save()
        {
            bool ans = false;
            try
            {
                string[] paramnames = c_Columns.Split(',');
                string[] paramvalues = new string[c_ColumnsCount];
                paramvalues[0] = viotype.ToString();
                paramvalues[1] = viocode;
                paramvalues[2] = viodescribe;
                paramvalues[3] = bz;
                ans = DBHelper.GetInstance().EditorAddParam(c_TableName, paramnames, paramvalues);
            }
            catch { ans = false; }
            return ans;
        }
    }
}
